package view;
import dao.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import commen.*;
@WebServlet("/login")
public class login_Servlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        //1、看用户名密码是不是空
        String username=req.getParameter("username");
        String password=req.getParameter("password");
        if(username==null||username.equals("")||password==null||password.equals("")){
            String html="密码或用户名不正确";
            resp.getWriter().write(html);
            return;
        }
        //2、从数据库取出密码
        User user=UserDao.selectByName(username);
        if(user==null){
            String html="密码或用户名不正确";
            resp.getWriter().write(html);
            return;
        }
        String password2=user.getPassword();
        //3、对比密码是否一样
        if(!password.equals(password2)){
            String html="密码或用户名不正确";
            resp.getWriter().write(html);
            return;
        }
        //4.登录成功，就把当前对象存到httpsession中
        HttpSession session=req.getSession(true);
        session.setAttribute("user",user);
        //5.跳转主页
        resp.sendRedirect("blog_list.html");
    }
}
